<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="/WEB-INF/page-base.tld" prefix="p"%>

<!DOCTYPE html>
<HTML>
  <head>
	<%@ include file="/platform/style.jsp"%>
	<script src="<%=path %>/util/js/combox.js"></script>
	<script src='<%=path %>/util/js/ui.core.js' type='text/javascript'></script>
	<link href='<%=path %>/util/js/dtree/skin/ui.dynatree.css' rel='stylesheet' type='text/css'>
	<script src='<%=path %>/util/js/dtree/jquery.dynatree.js' type='text/javascript'></script>
<script>
	$(function(){
		$("#userdepttree").tree({
			url: ctx+"/platform/dept/DeptPage@initDeptTree.page",
			onBeforeExpand:function(node){
				$('#userdepttree').tree('options').url = ctx+"/platform/dept/DeptPage@loadDeptTree.page?deptId="+node.id;
			},
			onClick: function(node){
				$(this).tree('toggle', node.target);
			},
			onSelect:function(node){
				$getByName("deptman_deptid").val(node.id);
				useradmin_showList(ctx+"/platform/user/UserManagePage.page?deptId="+node.id);
			},
			onContextMenu: function(e,node){
				e.preventDefault();
				$getByName("deptman_deptid").val(node.id);
				$('#deptMenu').menu('show',{
					left: e.pageX,
					top: e.pageY
				});
			}
		})
		useradmin_showList(ctx+"/platform/user/UserManagePage.page?deptId=1");
	})
	function useradmin_showList(url){
		$w.execUrl(url,{},$('#useradminListPanel'));
	}
	function dept_adddept(){
		if($getByName("deptman_deptid").val()==""){
			window.parent.$w.infoMsg('请先选择一个上级机构！');
			return false;
		}
		var str=ctx+"/platform/dept/DeptPage.page?actions=addShow&deptId="+$getByName("deptman_deptid").val();
		window.parent.$w.openDialog({title:'新增机构',w:420,h:280,url:str,afterClose:function(re){
			if(re){
				var node=$('#userdepttree').tree("find",$getByName("deptman_deptid").val());
				var data=JSON.parse(re);
				$('#userdepttree').tree("append",{
					parent: node.target,
					data:[{id:data.id,text:data.text,iconUrl:data.iconUrl}]
				});
				$('#userdepttree').tree("expand",node.target);
			}
		}});
	}
	function dept_editdept(){
		var values=$getByName("deptman_deptid").val();
		if(values==""){
			window.parent.$w.infoMsg('请选择一个机构！');
			return false;
		}
		var str=ctx+"/platform/dept/DeptPage.page?actions=updateShow&deptId="+values;
		window.parent.$w.openDialog({title:'修改机构',w:420,h:280,url:str,afterClose:function(re){
			if(re){
				var node=$('#userdepttree').tree("find",$getByName("deptman_deptid").val());
				$('#userdepttree').tree("update",{target:node.target,text:re});
			}
		}});
	}
	function dept_deldept(){
		var values=$getByName("deptman_deptid").val();
		if(values==""){
			window.parent.$w.infoMsg('请选择一个机构！');
			return false;
		}else if(values=='1'){
			window.parent.$w.infoMsg('不能删除根节点！');
			return false;
		}
		var urls=ctx+"/platform/dept/DeptPage.page?actions=del&deptId="+values;
		window.parent.$w.confirmMsg('确定删除？',function(r){
			if(r){
				$w.doUrl(urls,{},function(re){
					if(re.state=='ok'){
						var node=$('#userdepttree').tree("find",$getByName("deptman_deptid").val());
						$('#userdepttree').tree("remove",node.target);
						$getByName("deptman_deptid").val("");
					}
				})
			}
		});
	}
</script>
</head>
<body class="easyui-layout">
<div data-options="region:'center',border:false">
	<input name="deptman_deptid" type="hidden">
	<div class="easyui-layout" data-options="fit:true">
		<div data-options="region:'west',split:true" title="机构目录" style="width:220px">
			<div class="easyui-panel ui-toolBar" border="false" data-options="doSize:false">
				<a href="javascript:;" class="easyui-linkbutton" title="新增子机构" iconCls="icon_add" plain="true" onclick="dept_adddept()">添加</a>
				<a href="javascript:;" class="easyui-linkbutton" title="修改已选机构" iconCls="icon_edit" plain="true" onclick="dept_editdept()">修改</a>
				<a href="javascript:;" class="easyui-linkbutton" title="删除已选机构" iconCls="icon_del" plain="true" onclick="dept_deldept()">删除</a>
			</div>
			<div id="userdepttree"></div>
		</div>
		
		<div data-options="region:'center',split:true">
			<div id="useradminListPanel" class="easyui-panel" border="false" data-options="doSize:false">
			</div>
		</div>
	</div>
	<div id="deptMenu" class="easyui-menu" style="width:100px;">  
	       <div onclick="dept_adddept()" data-options="iconCls:'icon_add'">添加子机构</div>
	       <div onclick="dept_editdept()" data-options="iconCls:'icon_edit'">修改本机构</div>
	       <div onclick="dept_deldept()" data-options="iconCls:'icon_del'">删除本机构</div>
	</div>
</div>
  </body>
</html>